package main.Q1301_1400;

import java.util.*;

public class Q1301_1310 {
    public static void main(String[] args) {
        System.out.println("Question1301：");
        System.out.println("Question1302：层数最深叶子节点的和");
        System.out.println("Question1303：");
        System.out.println("Question1304：");
        System.out.println("Question1305：两棵二叉搜索书中的所有元素");
        System.out.println("Question1306：");
        System.out.println("Question1307：");
        System.out.println("Question1308：");
        System.out.println("Question1309：解码字母到整数映射");
        System.out.println("Question1310：子数组异或查询");
        int[] num = new int[]{4, 8, 2, 10};
        int[][] qu = new int[][]{{2, 3}, {1, 3}, {0, 0}, {0, 3}};
        System.out.println(new Question1310().xorQueries(num, qu));
    }
}

class Question1302 {
    int deep = -1, sum = 0;

    public void process(TreeNode root, int level) {
        if (root.left == null && root.right == null) {//叶子节点
            if (level > deep) {
                deep = level;
                sum = root.val;
            } else if (level == deep) {
                sum += root.val;
            }
        }
        if (root.right != null) process(root.right, level + 1);
        if (root.left != null) process(root.left, level + 1);
    }

    public int deepestLeavesSum(TreeNode root) {
        process(root, 1);
        return sum;
    }
}

class Question1305 {
    public List<Integer> getAllElements(TreeNode root1, TreeNode root2) {
        List<Integer> result = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        if (root1 != null) {
            queue.add(root1);
            while (!queue.isEmpty()) {
                root1 = queue.poll();
                result.add(root1.val);
                if (root1.left != null) queue.add(root1.left);
                if (root1.right != null) queue.add(root1.right);
            }
        }
        if (root2 != null) {
            queue.add(root2);
            while (!queue.isEmpty()) {
                root2 = queue.poll();
                result.add(root2.val);
                if (root2.left != null) queue.add(root2.left);
                if (root2.right != null) queue.add(root2.right);
            }
        }
        Collections.sort(result);
        return result;
    }
}

class Question1309 {
    public String freqAlphabets(String s) {
        StringBuffer result = new StringBuffer();
        int i = s.length() - 1;
        char c = ' ';
        while (i >= 0) {
            if (s.charAt(i) == '#') {
                c = (char) (Integer.valueOf(s.substring(i - 2, i)) + 96);
                result.insert(0, c);
                i -= 3;
            } else {
                c = (char) ((int) s.charAt(i) + 48);
                result.insert(0, c);
                i--;
            }
        }
        return result.toString();
    }
}

class Question1310 {
    public int[] xorQueries(int[] arr, int[][] queries) {
        int[] dp = new int[arr.length];
        dp[0] = arr[0];
        for (int i = 1; i < arr.length; i++) {
            dp[i] = dp[i - 1] ^ arr[i];
        }
        int[] result = new int[queries.length];
        for (int i = 0; i < result.length; i++) {
            int start = queries[i][0], end = queries[i][1];
            if (start != end) {
                if (start == 0) {
                    result[i] = dp[end];
                } else {
                    result[i] = dp[end] ^ dp[start - 1];
                }
            } else {
                result[i] = arr[start];
            }
        }
        return result;
    }
}